package com.wjp.dinosaur.controller;

import com.alibaba.fastjson.JSON;
import com.wjp.dinosaur.service.ScoreService;
import com.wjp.dinosaur.util.UserIdUtil;
import com.wjp.dinosaur.vo.Result;
import com.wjp.dinosaur.vo.parm.Score;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RequestMapping("score")
@RestController
public class ScoreController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private ScoreService scoreService;

    @CrossOrigin
    @PostMapping("update")
    public Result updateScore(@RequestBody Score score) {
        int score2 = score.getScore();
        Long userId = UserIdUtil.getUserId(httpServletRequest);
        redisTemplate.opsForValue().set("ACCOUNT_" + userId, JSON.toJSONString(score2), 1000, TimeUnit.DAYS);
        return Result.success(true);
    }

    @CrossOrigin
    @GetMapping("get")
    public Result getScore() {
        return scoreService.getScore();
    }

    @GetMapping("getMe")
    @CrossOrigin
    public Result getMe(){
        return scoreService.getMe();
    }

}
